package westeel.dataserver.controller;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import westeel.dataserver.config.Security;
import westeel.dataserver.request.*;
import westeel.dataserver.service.FreeQueryService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FreeQueryController {
    @Autowired
    Security security;

    @Autowired
    @Qualifier("freeQueryService")
    private FreeQueryService freeQueryService;

    @PostMapping("/freeQuerySql")
    public Map<String,Object> freeQuerySql(@RequestBody FreeQuery request) throws SQLException {
       String dataSourceName = request.getDataSource();
       freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
       List result = freeQueryService.query(dataSourceName,request.getSql()); //返回查询请求到的数据
       Map map =  new HashMap<>();
       map.put("result",result);
       map.put("code",0);
       return map;
    }
    @PostMapping("/freeQuerySqlWithParams")
    public Map<String,Object> freeQuerySqlWithParams(@RequestBody FreeQuery request) throws SQLException {
        String dataSourceName = request.getDataSource();
        freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
        List result = freeQueryService.queryWithParams(dataSourceName,request.getSql(),request.getParams()); //返回查询请求到的数据
        Map map =  new HashMap<>();
        map.put("result",result);
        map.put("code",0);
        return map;
    }

    @PostMapping("/freeUpdateSql")
    public Map<String,Object> freeUpdateSql(@RequestBody FreeQuery request) throws SQLException {
        String dataSourceName = request.getDataSource();
        freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
        long affectedCount = freeQueryService.update(dataSourceName,request.getSql()); //返回查询请求到的数据
        Map map =  new HashMap<>();
        map.put("result",affectedCount);
        map.put("code",0);
        return map;
    }
    @PostMapping("/freeUpdateSqlWithParams")
    public Map<String,Object> freeUpdateSqlWithParams(@RequestBody FreeQuery request) throws SQLException {
        String dataSourceName = request.getDataSource();
        freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
        long affectedCount = freeQueryService.updateWithParams(dataSourceName,request.getSql(),request.getParams()); //返回查询请求到的数据
        Map map =  new HashMap<>();
        map.put("result",affectedCount);
        map.put("code",0);
        return map;
    }

    @PostMapping("/createDataSource")
    public Map createDataSource(@RequestBody CreateDataSourceRequest request){
        String dataSourceName = request.getName();
        Map result = new HashedMap(); //返回查询请求到的数据
        try {
            freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
            freeQueryService.createDataSource(dataSourceName,request.getProps());
            result.put("code",0);
            result.put("msg","succeed to create datasource");
        } catch (RuntimeException e) {
            result.put("code",-1);
            result.put("error",e.getMessage());
        }
        return result;
    }
    @PostMapping("/removeDataSource")
    public Map createDataSource(@RequestBody RemoveDataSourceRequest request){
        String dataSourceName = request.getName();
        Map result = new HashedMap(); //返回查询请求到的数据
        try {
            freeQueryService.reportErrIfDataSourceIsSystem(dataSourceName);
            freeQueryService.removeDataSource(dataSourceName);
            result.put("code",0);
            result.put("msg","succeed to remove datasource");
        } catch (RuntimeException e) {
            result.put("code",-1);
            result.put("error",e.getMessage());
        }
        return result;
    }
    @PostMapping("/listSqlsByAdmin")
    public Map<String,Object> listSqlsByAdmin(@RequestBody ListSql request) throws SQLException {
        String password = request.getAdminPassword();
        if(!security.checkIsAdmin(password)){
            Map map =  new HashMap<>();
            map.put("result","no right!");
            map.put("code",0);
            return map;
        }
        List result = freeQueryService.query(freeQueryService.getSystemDataSourceName(),request.getKey() == null ? "SELECT key,sql,params FROM free_query_sql":"SELECT key,sql,params FROM free_query_sql WHERE key = '"+request.getKey()+"'"); //返回查询请求到的数据
        Map map =  new HashMap<>();
        map.put("result",result);
        map.put("code",0);
        return map;
    }
    @PostMapping("/removeSqlsByAdmin")
    public Map<String,Object> removeSqlsByAdmin(@RequestBody RemoveSql request) throws SQLException {
        String password = request.getAdminPassword();
        if(!security.checkIsAdmin(password)){
            Map map =  new HashMap<>();
            map.put("result","no right!");
            map.put("code",0);
            return map;
        }
        if(request.getKey() == null){
            Map map =  new HashMap<>();
            map.put("result","no key!");
            map.put("code",0);
            return map;
        }
        long affected = freeQueryService.removeSql(request.getKey());
        Map map =  new HashMap<>();
        map.put("result",affected);
        map.put("code",0);
        return map;
    }
    @PostMapping("/updateSqlByAdmin")
    public Map<String,Object> updateSqlByAdmin(@RequestBody UpdateSql request) throws SQLException {
        String password = request.getAdminPassword();
        if(!security.checkIsAdmin(password)){
            Map map =  new HashMap<>();
            map.put("result","no right!");
            map.put("code",0);
            return map;
        }
        if(request.getKey() == null){
            Map map =  new HashMap<>();
            map.put("result","no key!");
            map.put("code",0);
            return map;
        }
        long affected = freeQueryService.updateSql(request.getKey(),request.getSql(),request.getUserGroup());
        Map map =  new HashMap<>();
        map.put("result",affected);
        map.put("code",0);
        return map;
    }
    @PostMapping("/createSqlByAdmin")
    public Map<String,Object> createSqlByAdmin(@RequestBody CreateSql request) throws SQLException {
        String password = request.getAdminPassword();
        if(!security.checkIsAdmin(password)){
            Map map =  new HashMap<>();
            map.put("result","no right!");
            map.put("code",0);
            return map;
        }
        if(request.getKey() == null){
            Map map =  new HashMap<>();
            map.put("result","no key!");
            map.put("code",0);
            return map;
        }
        long affected = freeQueryService.createSql(request.getKey(),request.getSql(),request.getUserGroup());
        Map map =  new HashMap<>();
        map.put("result",affected);
        map.put("code",0);
        return map;
    }
}
